// pages/course_detail_qd/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getCourseDetailQr,getCourseDetailQrqd,getCourseClassQr,getCourseClassQrConfimg} from '../../api/index'
import {baseURL} from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    class:{},
    userInfo:{},
    // 1开课 2核销
    status:1,
    point_id:0
  },

  submitFn1(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      class_id:this.data.class.id,
      xuid:this.data.userInfo.id
    }
    getCourseDetailQrqd(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);
      }else{
        showText(res.msg,2)
      }
    })
  },
  submitFn2(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      point_id:this.data.point_id
    }
    getCourseClassQrConfimg(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);
      }else{
        showText(res.msg,2)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        status:options.id
      })
    }
    var userId = wx.getStorageSync('userId')
    var course_id = wx.getStorageSync('course_id')
    if(course_id){
      this.setData({
        point_id:course_id
      })
      wx.removeStorageSync('userId')
      wx.removeStorageSync('course_id')
      if(this.data.status == 1){
        var obj = {
          uid:wx.getStorageSync('uid'),  
          xuid:userId,
          class_id:course_id
        }
        getCourseDetailQr(obj).then(res=>{
          if(res.sta == 1){
            if(res.class.images){
              var arr1 = []
              res.class.images = res.class.images.split(',')
              res.class.images.forEach(item=>{
                arr1.push(baseURL + item)
              })
              res.class.images = arr1
            }
            this.setData({
              userInfo:res.userinfo,
              class:res.class
            })
          }
        })
      }else if(this.data.status == 2){
        var obj = {
          uid:wx.getStorageSync('uid'),
          point_id:course_id
        }
        getCourseClassQr(obj).then(res=>{
          if(res.sta == 1){
            if(res.class.images){
              var arr1 = []
              res.class.images = res.class.images.split(',')
              res.class.images.forEach(item=>{
                arr1.push(baseURL + item)
              })
              res.class.images = arr1
            }
            this.setData({
              userInfo:res.user,
              class:res.class
            })
          }
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})